package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import model.GameConfig;


public class GameStartConfig extends JFrame {

	/**
	 * 
	 */
	//private static final long serialVersionUID = 1L;
	private int numPlayers;
	private int numTotalP;
	private int numSimpleAI;
	private int numExpertAI;
	private boolean randomAssign;
	private Object lock;
	/**
	 * Sets the game options when creating a new game to run
	 * sets the # of Human, SimpleAI, ExpertAI, randomAssignment, and map
	 * @param l
	 */
	public GameStartConfig(Object l) {
		/*
		 * Kevin - Using a lock Object so whatever creates the GameStartConfig
		 * window will know when it can access the two ints. If you have another way to do
		 * this on the server side go ahead and do it that way. This was just for my tests.
		 */
		
		lock = l;

		numPlayers = 0;
		numTotalP = 0;

		this.setTitle("Select Game Options");
		this.setMinimumSize(new Dimension(652, 400));
		// .setMaximumSize(new Dimension(1200,800));
		// frame.setPreferredSize(new Dimension(200,600));

		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

		JPanel buttons = new JPanel();
		buttons.setLayout(new FlowLayout());

		JLabel buttonLabel = new JLabel("How many HUMAN players?");
		buttons.add(buttonLabel);

		// Ask for number of human players
		JRadioButton one = new JRadioButton("One");
		one.addActionListener(new PlayerButtonListener(1, 1));
		JRadioButton two = new JRadioButton("Two");
		two.addActionListener(new PlayerButtonListener(2, 1));
		JRadioButton three = new JRadioButton("Three");
		three.addActionListener(new PlayerButtonListener(3, 1));
		JRadioButton four = new JRadioButton("Four");
		four.addActionListener(new PlayerButtonListener(4, 1));
		JRadioButton five = new JRadioButton("Five");
		five.addActionListener(new PlayerButtonListener(5, 1));
		JRadioButton six = new JRadioButton("Six");
		six.addActionListener(new PlayerButtonListener(6, 1));
		// Group the radio buttons.
		ButtonGroup group = new ButtonGroup();
		group.add(one);
		group.add(two);
		group.add(three);
		group.add(four);
		group.add(five);
		group.add(six);
		// add buttons to panel
		buttons.add(one);
		buttons.add(two);
		buttons.add(three);
		buttons.add(four);
		buttons.add(five);
		buttons.add(six);
		
		
		
		// Ask for numSimpleAI
		JPanel buttonsS = new JPanel();
		buttonsS.setLayout(new FlowLayout());

		JLabel buttonSLabel = new JLabel("How many SimpleAI players?");
		buttonsS.add(buttonSLabel);

		JRadioButton zeroS = new JRadioButton("Zero");
		zeroS.addActionListener(new PlayerButtonListener(0, 3));
		JRadioButton oneS = new JRadioButton("One");
		oneS.addActionListener(new PlayerButtonListener(1, 3));
		JRadioButton twoS = new JRadioButton("Two");
		twoS.addActionListener(new PlayerButtonListener(2, 3));
		JRadioButton threeS = new JRadioButton("Three");
		threeS.addActionListener(new PlayerButtonListener(3, 3));
		JRadioButton fourS = new JRadioButton("Four");
		fourS.addActionListener(new PlayerButtonListener(4, 3));
		JRadioButton fiveS = new JRadioButton("Five");
		fiveS.addActionListener(new PlayerButtonListener(5, 3));
		JRadioButton sixS = new JRadioButton("Six");
		sixS.addActionListener(new PlayerButtonListener(6, 3));
		// Group the radio buttons.
		ButtonGroup groupS = new ButtonGroup();
		groupS.add(zeroS);
		groupS.add(oneS);
		groupS.add(twoS);
		groupS.add(threeS);
		groupS.add(fourS);
		groupS.add(fiveS);
		groupS.add(sixS);
		// add buttons to panel
		buttonsS.add(zeroS);
		buttonsS.add(oneS);
		buttonsS.add(twoS);
		buttonsS.add(threeS);
		buttonsS.add(fourS);
		buttonsS.add(fiveS);
		buttonsS.add(sixS);
		
		// Ask for numSimpleAI
		JPanel buttonsE = new JPanel();
		buttonsE.setLayout(new FlowLayout());

		JLabel buttonELabel = new JLabel("How many ExpertAI players?");
		buttonsE.add(buttonELabel);

		JRadioButton zeroE = new JRadioButton("Zero");
		zeroE.addActionListener(new PlayerButtonListener(0, 4));
		JRadioButton oneE = new JRadioButton("One");
		oneE.addActionListener(new PlayerButtonListener(1, 4));
		JRadioButton twoE = new JRadioButton("Two");
		twoE.addActionListener(new PlayerButtonListener(2, 4));
		JRadioButton threeE = new JRadioButton("Three");
		threeE.addActionListener(new PlayerButtonListener(3, 4));
		JRadioButton fourE = new JRadioButton("Four");
		fourE.addActionListener(new PlayerButtonListener(4, 4));
		JRadioButton fiveE = new JRadioButton("Five");
		fiveE.addActionListener(new PlayerButtonListener(5, 4));
		JRadioButton sixE = new JRadioButton("Six");
		sixE.addActionListener(new PlayerButtonListener(6, 4));
		// Group the radio buttons.
		ButtonGroup groupE = new ButtonGroup();
		groupE.add(zeroE);
		groupE.add(oneE);
		groupE.add(twoE);
		groupE.add(threeE);
		groupE.add(fourE);
		groupE.add(fiveE);
		groupE.add(sixE);
		// add buttons to panel
		buttonsE.add(zeroE);
		buttonsE.add(oneE);
		buttonsE.add(twoE);
		buttonsE.add(threeE);
		buttonsE.add(fourE);
		buttonsE.add(fiveE);
		buttonsE.add(sixE);

		JCheckBox randomBox = new JCheckBox();
		randomBox.addItemListener(new RandomSelectListener());
		JLabel randomLabel = new JLabel("Randomly Assign Territories?");
		JPanel randomP = new JPanel();
		randomP.add(randomLabel, BorderLayout.CENTER);
		randomP.add(randomBox, BorderLayout.EAST);

		// JPanel buttonsGroup = new JPanel();
		// buttonsGroup.setLayout(new FlowLayout());
		// buttonsGroup.add(buttons);
		// buttonsGroup.add(buttonsT);

		JLabel image = new JLabel();
		image.setIcon(new ImageIcon("images/ClassicRiskLogo.png"));

		JButton classic = new JButton("Classic Map");
		classic.addActionListener(new ClassicActionListener());

		this.setLayout(new FlowLayout());
		this.add(buttons);
		this.add(buttonsS);
		this.add(buttonsE);
		
		this.add(randomP);
		this.add(image);
		this.add(classic);

		// frame.setVisible(true);
	}

	private class RandomSelectListener implements ItemListener {

		@Override
		public void itemStateChanged(ItemEvent arg0) {
			if( arg0.getStateChange() == ItemEvent.DESELECTED){
				randomAssign = false;
			}
			else{
				randomAssign = true;
			}
			
		}
		
	}
	
	
	private class PlayerButtonListener implements ActionListener {
		private int num;
		private int numChange;

		public PlayerButtonListener(int num, int numChange) {
			this.num = num;
			this.numChange = numChange;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			if (numChange == 1) {
				numPlayers = num;
			}
//			else if (numChange == 2) {
//				numTotalP = num;
//			}
			else if(numChange == 3){
				numSimpleAI = num;
			}
			else if(numChange == 4){
				numExpertAI = num;
			}
			numTotalP = numPlayers+numSimpleAI+numExpertAI;
		}
	}

	private class ClassicActionListener implements ActionListener {
		
		/*
		 * TODO Need to swap this over to send this data to back to
		 * the server instead of immediately populating the GUI.
		 */

		@Override
		public void actionPerformed(ActionEvent e) {
			if (numTotalP != 0) {
				if(numTotalP <7 ){
					if (numPlayers <= numTotalP) {
						setVisible(false);
						synchronized(lock){
							lock.notifyAll();
						}
					}
					else {
						JOptionPane.showMessageDialog(null,
							"More HUMANS than TOTAL players? LOL");
					}
				}
				else {
					JOptionPane.showMessageDialog(null,
					"No more than 6 TOTAL Player");
				}
			} 
			else {
				JOptionPane
						.showMessageDialog(null, "Pick Number Of Players!!!");
			}
		}
	}

	public GameConfig getGameConfig() {
		return new GameConfig( numPlayers, numTotalP, numSimpleAI, numExpertAI, randomAssign );
	}

}
